<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/23 14:06
 * Modified Time: 2019/7/23 14:06
 * Version: 2.0
 */

namespace App\Modules\System\Services;
use App\Exceptions\AllValidatorException;
use App\Lib\File\AliOssVideo;
use App\Models\Video;
use Flower\Core\Repository\Eloquent\BaseService;

class VideoService extends BaseService
{


    public function upload($requestData){
        $rules = [
            'file' => 'required|mimetypes:video/avi,video/mpeg,video/quicktime,video/mp4'
        ];
        $message = [
            'file.required' => '请上传正确格式的视频',
            'file.mimetypes' => '请上传正确格式的视频'
        ];

        $validator = \Validator::make($requestData,$rules,$message);

//        if(!$validator->passes()) {//通过表单验证
//            $msg = array_values($validator->getMessageBag()->getMessages())[0];
//            throw new AllValidatorException(['msg'=>$msg]);
//        }


        $file = $requestData['file'];

        $oss = new AliOssVideo();
        return $oss->upload($file,'test');

    }

    /**
     * 通过OSS URL 添加记录
     * @param $oss_url
     * @return Video
     */
    public function addByOSSUrl($oss_url){
        $new_video         = new Video();
        $new_video->name   = '视频';
        $new_video->url    = $oss_url;
        $new_video->thumb  = $oss_url . "?x-oss-process=video/snapshot,t_1,f_jpg,m_fast,ar_auto";
        $new_video->status = 1;
        $new_video->save();
        return $new_video;
    }
}